package com.promotores;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class GestorTipoPromocion extends CreaTablas{
	CreaTablas db;	
	private static final String TABLE_TIPOPROMOCION = "TipoPromocion";	
	private static final String KEY_ID = "id";
	private static final String KEY_TIPO_PROMOCION = "tipoPromocion";
	private static final String KEY_FLAG_SAVED = "flagSaved";
	private static final String KEY_DELETED = "deleted";
	private static final String KEY_CREATED_AT = "created_at";
	private static final String KEY_UPDATED_AT = "updated_at";
	
	public GestorTipoPromocion(Context context) {
		super(context);
		db = new CreaTablas(context);
	}

	/**
	 * All CRUD(Create, Read, Update, Delete) Operations
	 */

	// Adding new tipoPromocion
	public void addTipoPromocion(BeanTipoPromocion tipoPromocion) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(KEY_ID, tipoPromocion.getId());
		values.put(KEY_TIPO_PROMOCION, tipoPromocion.getTipoPromocion());
		values.put(KEY_FLAG_SAVED, tipoPromocion.getSaved());
		values.put(KEY_DELETED, tipoPromocion.getDeleted());
		values.put(KEY_CREATED_AT, tipoPromocion.getCreated_at());
		values.put(KEY_UPDATED_AT, tipoPromocion.getUpdated_at());
		// Inserting Row
		db.insert(TABLE_TIPOPROMOCION, null, values);
		db.close(); // Closing database connection
	}

	// Getting single tipoPromocion
	public BeanTipoPromocion getTipoPromocion(int id) {
		SQLiteDatabase db = this.getReadableDatabase();
		String selectQuery = "SELECT  * FROM " + TABLE_TIPOPROMOCION + " WHERE " + KEY_ID + "=" + id + ";";
		Cursor cursor = db.rawQuery(selectQuery, null);
		if (cursor != null)
			cursor.moveToFirst();

		BeanTipoPromocion tipoPromocion = new BeanTipoPromocion();
		tipoPromocion.setId(Integer.parseInt(cursor.getString(0)));
		tipoPromocion.setTipoPromocion(cursor.getString(1));
		tipoPromocion.setSaved(cursor.getString(2));
		tipoPromocion.setDeleted(cursor.getString(3));
		tipoPromocion.setCreated_at(cursor.getString(4));
		tipoPromocion.setUpdated_at(cursor.getString(5));
		db.close();
		return tipoPromocion;
	}
	

	// Getting All tipoPromocion
	public List<BeanTipoPromocion> getAlltipoPromocion() {
		List<BeanTipoPromocion> listaTipoPromociones = new ArrayList<BeanTipoPromocion>();
		// Select All Query
		String selectQuery = "SELECT  * FROM " + TABLE_TIPOPROMOCION + " WHERE " +  KEY_DELETED + " <> '1'" ;
		//String selectQuery = "SELECT  * FROM " + TABLE_TIPOPROMOCION + " ;";

		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);

		// looping through all rows and adding to list
		if (cursor.moveToFirst()) {
			do {
				BeanTipoPromocion tipoPromocion = new BeanTipoPromocion();
				tipoPromocion.setId(Integer.parseInt(cursor.getString(0)));
				tipoPromocion.setTipoPromocion(cursor.getString(1));
				tipoPromocion.setSaved(cursor.getString(2));
				tipoPromocion.setDeleted(cursor.getString(3));
				tipoPromocion.setCreated_at(cursor.getString(4));
				tipoPromocion.setUpdated_at(cursor.getString(5));
				// Adding tipoPromocion to list
				listaTipoPromociones.add(tipoPromocion);
			} while (cursor.moveToNext());
		}
		
		db.close();
		// return tipoPromocion list
		return listaTipoPromociones;
	}

	// Updating single tipoPromocion
	public void updateTipoPromocion(BeanTipoPromocion tipoPromocion) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.execSQL("UPDATE " + TABLE_TIPOPROMOCION + " SET " + KEY_TIPO_PROMOCION + "='" + tipoPromocion.getTipoPromocion() + "', " 
				+ KEY_UPDATED_AT + "='" + tipoPromocion.getUpdated_at() + "', " + KEY_DELETED + "='" + tipoPromocion.getDeleted() +
				"' WHERE " + KEY_ID + "=" + tipoPromocion.getId());	
		db.close();
	}

	// Deleting single tipoPromocion
	public void deleteTipoPromocion(BeanTipoPromocion tipoPromocion) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(TABLE_TIPOPROMOCION, KEY_ID + " = ?",
				new String[] { String.valueOf(tipoPromocion.getId()) });
		db.close();
	}

	// Getting tipoPromocion Count
	public int getTipoPromocionCount() {
		String countQuery = "SELECT  * FROM " + TABLE_TIPOPROMOCION;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(countQuery, null);
		cursor.close();

		// return count
		return cursor.getCount();
	}

	public void deleteAll(){
		SQLiteDatabase db = this.getWritableDatabase();
		db.execSQL("DELETE FROM " + TABLE_TIPOPROMOCION);	
		db.close();
	}

	public List<BeanTipoPromocion> getTipoPromocionbyFiltro(String keyword) {
		// TODO Auto-generated method stub
		List<BeanTipoPromocion> lstTipoPromocion = new ArrayList<BeanTipoPromocion>();
		
		String selectQuery = "SELECT  * FROM " + TABLE_TIPOPROMOCION + " WHERE " + KEY_TIPO_PROMOCION + " LIKE '%" + keyword + "%' ;";

		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);
		
		if (cursor.moveToFirst()) {
			do {
				BeanTipoPromocion tipoPromocion = new BeanTipoPromocion();
				tipoPromocion.setId(Integer.parseInt(cursor.getString(0)));
				tipoPromocion.setTipoPromocion(cursor.getString(1));
				tipoPromocion.setSaved(cursor.getString(2));
				tipoPromocion.setDeleted(cursor.getString(3));
				tipoPromocion.setCreated_at(cursor.getString(4));
				tipoPromocion.setUpdated_at(cursor.getString(5));
				lstTipoPromocion.add(tipoPromocion);
			} while(cursor.moveToNext());
		}
		cursor.close();
		db.close();
		return lstTipoPromocion;
	}	
	
}